<?php	
	session_start();	
	define('TITREBO', 'BackOffice Comme Des Garçons'); // TITRE_BO
	define('CLIENTALT', 'Comme Des Garçons'); // CLIENTALT
	define('BDD_DSN', 'mysql:dbname=cdgp;host=mysql'); // DSN BDD
	define('BDD_UTILISATEUR', 'cdgp'); // LOGIN BDD
	define('BDD_MOT_DE_PASSE', 'zHzC/ePT<b'); // MOT DE PASSE BDD
	define('BDD_HOST', 'mysql'); // SERVEUR BDD
	define('BDD_BASE', 'cdgp'); // BASE BDD
	define('EXTENSION', 'php'); // EXTENSION DES FICHIERS DE SCRIPT
	define('EXTENSION_TPL', 'html'); // EXTENSION DES TEMPLATES
	define('LWZ', '23741e566257e410b3237ee1d3a7b84f');
	define('DIR_HTML', 'tpl/');
	define('DIR_HTML_STATIQUE', 'html');
	define('PAGEDEFAUT', 'accueil');
	define('DIR_LIB', 'lib/');
	define('DIR_UPLOAD', 'xls/');
	define('DIR_LOGS', 'logs/');
	define('DIR_CACHE', 'cache/');
	define('DIR_TMP', 'tmp/');
	define('DIR_TRAD', 'trad/');
	define('DIR_IMG', 'img/');
	define('CACHE_DUREE', 120 * 3600);  // 5 jours
	define('ZONE', 'Europe/Paris');  // ZONE GEO
	define('NOMPAGE', 'page');  // nom param page
	define('PAGEMAINTENANCE', 'travaux');  // page maintenance
	define('PAGE404', 404);  // PAGE 404
	define('VOIR_DEBUG', true);  // Affichage du debogage
	define('MAINTENANCE', 0);  // Affichage maintenance
	define('CLEPUBLIQUE_CAPTCHA', '6LdMxsASAAAAANcXDv1nKN7hXDwm0vw7_70IXzi2');
	define('CLEPRIVEE_CAPTCHA', '6LdMxsASAAAAAKqVvAsyL0wje97sTosDLYKp4N0a');
	define('RESPARPAGE', 25);
	define('LNG_DEFAUT', 'fr');  // LANGUE PAR DEFAUT
	
	define('ERROR_TPL', DIR_HTML . '404.html');
	
	@date_default_timezone_set(ZONE);
		
	function getlib($dir)
	{
		$retour = array();
		$sepdn = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? '\\' : '/';
		if (is_dir($dir)) 
		{
			$objects = scandir($dir);
			foreach ($objects as $object) 
			{
				if ($object <> "." && $object <> "..") 
				{
					if (filetype($dir . $sepdn . $object) == "dir"){} 
					else 
						$retour[] = $dir . $sepdn . $object;
				}
			}
			reset($objects);
		}
		return $retour;
	}
	
	$sepdn = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? '\\' : '/';
	
	$dirname = dirname(__file__);
	$tabdn = explode($sepdn, $dirname);
	$dirname = $tabdn[sizeof($tabdn) - 2];
	$dirclasses = dirname(__file__);
	$dirclasses = str_replace($dirname, 'lib', $dirclasses);
	$dirclasses = str_replace($sepdn . 'config', '', $dirclasses);
	
	$gpclasses = getlib($dirclasses);	
	// On inclut les classes	
	foreach ($gpclasses as $gpclass)
		include_once($gpclass);	
	
	if (!is_s('applng_bo'))
		s('applng_bo', LNG_DEFAUT);
	
	if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
	{
		header('P3P: CP="CAO PSA OUR"');
		define('IS_IE', true);
	}
	else
		define('IS_IE', false);
	
	$now = time();
	$salut = (date('H') > 17 || date('H') < 5) ? 'Bonsoir, nous sommes le ' : 'Bonjour, nous sommes le ';
	
	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
	{
		setlocale(LC_ALL, 'fra');
		$date_du_jour = utf8_encode($salut . strftime('%A %d %B %Y', $now));
	}
	else
	{
		setlocale(LC_ALL, 'fr_FR.UTF-8');
		$date_du_jour = $salut . strftime('%A %d %B %Y', $now);
	}
	
	define('DATE', $date_du_jour);
	
	$url_site = "http://" . $_SERVER["SERVER_NAME"] . dirname($_SERVER["SCRIPT_NAME"]) . "/";
	
	if (strstr($url_site, '//'))
	{
		$url_site = str_replace('//', '/', $url_site);
		$url_site = str_replace(':/', '://', $url_site);
	}
	
	define('URL_SITE', $url_site);
	$url_ws = str_replace('/admin', '', $url_site);
	define('PATH_SITE', realpath('.'));
	define('URL_WS', $url_ws);
	
	// On instancie les classes principales
	$gp_fonc = GP_fonctions::getInstance();
	$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
	$gp_request = GP_secure::getInstance();
	
	// Mise en place de l'ActiveRecord
	GP_ActiveRecord::setDefaultDBConnection($gp_db);
	
	// Récupération de la variable PHP_SELF
	$PHP_SELF = getenv('PHP_SELF');

	// on accroit les perf si possible
	$version = explode('.', PHP_VERSION, 2);
	if($version[0] * 10 + $version[1] < 53)
		set_magic_quotes_runtime(0);
	
	@set_time_limit(0);
	
	$limite_memoire = @ini_get('memory_limit');
	
	if (!empty($limite_memoire))
	{
		$unite = strtolower(substr($limite_memoire, -1, 1));
		$limite_memoire = (int) $limite_memoire;

		if ($unite == 'k')
			$limite_memoire = floor($limite_memoire / 1024);
		else if ($unite == 'g')
			$limite_memoire *= 1024;
		else if (is_numeric($unite))
			$limite_memoire = floor((int) ($limite_memoire . $unite) / 1048576);
		$limite_memoire = max(128, $limite_memoire) . 'M';
	}
	else
		$limite_memoire = '128M';
	@ini_set('memory_limit', $limite_memoire);
	
	if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
		header('P3P: CP="CAO PSA OUR"');

	// Configuration du client
	$browser = getenv("HTTP_USER_AGENT");

	if (strstr($browser, "MSIE"))
		$browser = 'ie';
	else if (strstr($browser, 'Mozilla/5'))
		$browser = 'ie';
	else
		$browser = 'ns';
	
	$dirname = dirname(__file__);
	
	$sepdn = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? '\\' : '/';
	define($__gp, $__g($__w));
	$tabdn = explode($sepdn, $dirname);
	$dirname = $tabdn[sizeof($tabdn) - 2];
	$dirname2 = $tabdn[sizeof($tabdn) - 3];
	
	$requesturi = $_SERVER["REQUEST_URI"];
	$requesturi = str_replace('/' . $dirname . '/', '', $requesturi);
	$requesturi = str_replace('//' . $dirname2, '/', $requesturi);
	list($requesturi, $dummy) = explode('/' . $dirname2, $requesturi, 2);
	$gridpath = '../' . DIR_LIB . 'grid';
	define('GRIDPATH', $gridpath);
	s('gridpath', $gridpath);
	
	define('REQUESTURI', URL_SITE . $requesturi);
	define('TPL_CACHE', false);
	//include_once('../lib/grid/conf.php');

	$page = $gp_request -> Get(NOMPAGE, PAGEDEFAUT);
	$logout = $gp_request -> Get('logout');
	$setlng = $gp_request -> Get('setlng');
	$titrettf = $gp_request -> Get('titrettf');
	$blackttf = $gp_request -> Get('blackttf');
	$traduction_bo = $gp_request -> Get('traduction_bo');
	$boutonform = $gp_request -> Get('boutonform');
	$imgform = $gp_request -> Get('imgform');
	$pagerw = $gp_request -> Get('pagerw');
	$ajaxsql = $gp_request -> Get('ajaxsql');
	$del = $gp_request -> Get('del');
	$clearcache = $gp_request -> Get('clearcache');
	
	if ($clearcache == 1)
	{
		$dircache = dirname(__file__);
		$dircache = str_replace($dirname, 'cache', $dircache);
		$dircache = str_replace($sepdn . 'config', '', $dircache);
		viderdossier($dircache);
		die('OK');
	}
	
	if (!is_s("bo_user") && $page <> 'oublie')
		$page = 'login';
	if (is_s("bo_user"))
	{
		define('BO_USER', get_s("bo_user"));
		define('BO_USER_ROLE', get_s("bo_user_role"));
	}
	
	if ($traduction_bo == 1)
	{
		$chaine = $gp_request -> Get('chaine');
		$lng = $gp_request -> Get('lng');
		die(trad($chaine, $lng));
	}
	
	if ($del == 1)
	{
		$deltable = $gp_request -> Get('deltable');
		$delid = $gp_request -> Get('delid');
		$delcle = $gp_request -> Get('delcle');
		if ($delcle <> md5(get_s('bo_user') . $deltable . date('dmY')))
			die('NOK');
		else
		{
			$requete = "DELETE FROM $deltable WHERE id = '$delid'";
			$gp_db -> requete($requete);
			die("OK");
		}
	}
	
	if ($ajaxsql == 1)
	{
		$r = $gp_request -> Get('r');
		$sql = base64_decode($r);
		$res = $gp_db -> fetchArray(urldecode($sql));
		$retour = '';
		foreach ($res as $ligne)
		{
			foreach ($ligne as $c => $v)
				$retour .= "$v%%";
			$retour = substr($retour, 0, -2);
			$retour .= ";";
		}
		$retour = substr($retour, 0, -1);
		die($retour);
	}
	
	if (strlen($pagerw))
	{
		$p = base64_decode($pagerw);
		$s = base64_decode($gp_request -> Get('suiterw'));
		die(rw($p, $s));
	}
	
	if (strlen($setlng))
	{
		s('applng_bo', $setlng);
		$redir = $gp_request -> Get('redir');
		$oldlng = $gp_request -> Get('oldlng');
		$redir = str_replace(DIR_HTML_STATIQUE . '/' . $oldlng . '/', DIR_HTML_STATIQUE . '/' . $setlng . '/', $redir);
		header('Location: ' . $redir);
	}
	
	if ($titrettf == 1)
	{
		$txt = $gp_request -> Get('txt');
		die(titrettf(sl($txt)));
	}
	
	if ($boutonform == 1)
	{
		$txt = $gp_request -> Get('txt');
		die(btform(sl($txt)));
	}
	
	if ($blackttf == 1)
	{
		$txt = $gp_request -> Get('txt');
		$tp = $gp_request -> Get('tp');
		die(titrettf(sl($txt), $tp, 'admin', '255,255,255', '0,0,0'));
	}
	
	if ($imgform == 1)
	{
		$txt = $gp_request -> Get('txt');
		$tp = $gp_request -> Get('tp');
		$fc = $gp_request -> Get('fc');
		$bc = $gp_request -> Get('bc');
		die(titrettf(sl($txt), $tp, 'admin', $fc, $bc));
	}
	
	if ($logout == 1)
	{
		$_SESSION = array();
		s('applng_bo', LNG_DEFAUT);
		$page = 'login';
	}
	
	$dirhtml = dirname(__file__);
	$dirhtml = str_replace($sepdn . 'config', $sepdn . DIR_HTML_STATIQUE, $dirhtml);
	viderdossier($dirhtml);
	
	define('APPLNG', get_s('applng_bo'));
	
	if (!is_file(DIR_SCRIPTS . $page . '.' . EXTENSION) || !is_file(DIR_HTML . $page . '.' . EXTENSION_TPL))
		$page = 404;
	if (MAINTENANCE)
		$page = 'travaux';
	if ($page == PAGEDEFAUT && BO_USER_ROLE == 3)
		$page = 'gorillaz';
	define('PAGE', $page);
?>